<template>
  <div class="app-container">
    <!-- 添加或修改生产报工记录对话框 -->
    <el-dialog v-if="feedBackOpen" :modal="false" :title="title" :visible.sync="feedBackOpen" width="1200px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工类型" prop="feedbackType">
              <el-select v-model="form.feedbackType" placeholder="请选择报工类型">
                <el-option v-for="dict in dict.type.mes_feedback_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产工单" prop="workorderCode">
              <el-input v-model="form.workorderCode" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产任务" prop="taskCode">
              <el-input v-model="form.taskCode" readonly="readonly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品编码" prop="itemCode">
              <el-input v-model="form.itemCode" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly="readonly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="计价方式" prop="pricingMethod">
              <el-select v-model="form.pricingMethod" placeholder="请选择来计价方式" readonly="readonly">
                <el-option v-for="dict in dict.type.pro_pricing_method" :key="dict.value" :disabled="true" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.pricingMethod == 'PIECEWORK'" :span="8">
            <el-form-item label="标准计件单价" prop="price">
              <el-input v-model="form.price" placeholder="请输入单价" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col v-else-if="form.pricingMethod == 'TIME'" :span="8">
            <el-form-item label="标准计时单价" prop="price">
              <el-input v-model="form.price" placeholder="请输入单价" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col v-if="form.pricingMethod == 'TIME'" :span="8">
            <el-form-item label="工作时长" prop="workingHours">
              <el-input-number v-model="form.workingHours" :min="1" :step="1" placeholder="请输入工作时长" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" readonly="readonly" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.isCheck == 'N'">
          <el-col :span="8">
            <el-form-item label="报工数量" prop="quantityFeedback">
              <el-input v-model="form.quantityFeedback" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合格品数量" prop="quantityQualified">
              <el-input-number v-model="form.quantityQualified" :min="0" placeholder="请输入合格品数量" @change="handleQuantityChanged" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="不良品数量" prop="quantityUnquanlified">
              <el-input-number v-model="form.quantityUnquanlified" :min="0" placeholder="请输入不良品数量" @change="handleQuantityChanged" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-else>
          <el-col :span="8">
            <el-form-item label="报工数量" prop="quantityFeedback">
              <el-input v-model="form.quantityFeedback" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="待检测数量" prop="quantityUncheck">
              <el-input-number v-model="form.quantityUncheck" :min="0" placeholder="请输入待检测数量" @change="handleQuantityChanged" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工人" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请选择报工人" readonly="readonly" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报工时间" prop="feedbackTime">
              <el-date-picker v-model="form.feedbackTime" clearable placeholder="请选择报工时间" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8" v-if="form.isPra != 'N'">
                      <el-form-item label="审核人" prop="recordNick">
                        <el-input v-model="form.recordNick" placeholder="请选择审核人">
                          <el-button slot="append" @click="handleUser2Select" icon="el-icon-search"></el-button>
                        </el-input>
                      </el-form-item>
                      <UserSingleSelect ref="user2Select" @onSelected="onUser2Selected"></UserSingleSelect>
                    </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入内容" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button v-hasPermi="['mes:pro:feedback:tijiao']" type="primary" @click="handleSubmit">保存并提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UserSingleSelect from '@/components/userSelect/single.vue'
import { getTaskInfo } from '@/api/mes/rr/rrtask'
import { addFeedback } from '@/api/mes/rr/rrfeedback'

export default {
  name: 'rrFeedBack',
  components: {
    UserSingleSelect
  },
  dicts: ['mes_feedback_type', 'pro_pricing_method'],
  data() {
    return {
      // 弹出层标题
      title: '返修报工',
      feedBackOpen: false,
      form: {
        recordId: null,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        routeId: null,
        routeCode: null,
        processId: null,
        processCode: null,
        processName: null,
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        rrTaskId: null,
        rrTaskCode: null,
        rrTaskName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: 0,
        quantityUncheck: 0,
        quantityFeedback: 0,
        quantityQualified: 0,
        quantityUnquanlified: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: new Date(),
        recordUser: null,
        recordNick: null,
        status: 'CONFIRMED',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        workingHours: 1
      },
      // 表单校验
      rules: {
        feedbackType: [{ required: true, message: '报工类型不能为空', trigger: 'change' }],
        rrTaskCode: [{ required: true, message: '请选择生产任务', trigger: 'blur' }],
        workorderCode: [{ required: true, message: '生产工单不能为空', trigger: 'blur' }],
        nickName: [{ required: true, message: '请选择报工人', trigger: 'blur' }],
        feedbackTime: [{ required: true, message: '请选择报工时间', trigger: 'blur' }]
      }
    }
  },

  methods: {
    init(rrTaskId) {
      getTaskInfo(rrTaskId).then((response) => {
        if (response.code == 200 && response.data) {
          this.taskInfo = response.data.taskInfo
          this.workorderInfo = response.data.workorderInfo

          this.form.taskId = this.taskInfo.rrTaskId
          this.form.taskCode = this.taskInfo.rrTaskCode
          this.form.taskName = this.taskInfo.rrTaskName
          this.form.workstationId = this.taskInfo.workstationId
          this.form.workstationCode = this.taskInfo.workstationCode
          this.form.workstationName = this.taskInfo.workstationName
          this.form.processId = this.taskInfo.processId
          this.form.processCode = this.taskInfo.processCode
          this.form.processName = this.taskInfo.processName
          this.form.isCheck = this.taskInfo.isCheck
          this.form.nickName = this.taskInfo.attr1
          this.form.userName = this.taskInfo.attr3
          this.form.pricingMethod = this.taskInfo.pricingMethod
          this.form.price = this.taskInfo.price
          this.form.isPra = this.taskInfo.isPra

          this.form.quantityFeedback = 0
          this.form.quantityUncheck = 0
          this.form.quantityQualified = 0
          this.form.quantityUnquanlified = 0

          this.form.workorderId = this.workorderInfo.id
          this.form.workorderCode = this.workorderInfo.repairCode
          this.form.workorderName = this.workorderInfo.workorderName
          this.form.itemId = this.workorderInfo.itemId
          this.form.itemCode = this.workorderInfo.itemCode
          this.form.itemName = this.workorderInfo.itemName
          this.form.specification = this.workorderInfo.specification
          this.form.unitOfMeasure = this.workorderInfo.unitOfMeasure
          this.feedBackOpen = true
        }
      })
    },
    // 取消按钮
    cancel() {
      this.feedBackOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        processId: null,
        processCode: null,
        processName: null,
        workorderId: null,
        workorderCode: null,
        rrTaskId: null,
        rrTaskCode: null,
        rrTaskName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: 0,
        quantityUncheck: 0,
        quantityFeedback: 0,
        quantityQualified: 0,
        quantityUnquanlified: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: new Date(),
        recordUser: null,
        recordNick: null,
        status: 'CONFIRMED',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        workingHours: 1
      }
      this.resetForm('form')
    },

    //点击人员选择按钮
    handleUserSelect() {
      this.$refs.userSelect.showFlag = true
    },
    //人员选择返回
    onUserSelected(row) {
      this.form.nickName = row.nickName
      this.form.userName = row.userName
    },
    //点击人员选择按钮
    handleUser2Select() {
      this.$refs.user2Select.showFlag = true
    },
    //人员选择返回
    onUser2Selected(row) {
      this.form.recordUser = row.userName
      this.form.recordNick = row.nickName
    },

    handleQuantityChanged() {
      this.form.quantityFeedback = this.form.quantityQualified + this.form.quantityUnquanlified + this.form.quantityUncheck
    },

    handleSubmit() {
      let that = this;
      that.$refs['form'].validate((valid) => {
        if (valid) {
          that.form.status = 'CONFIRMED'
          addFeedback(that.form).then((response) => {
            that.$modal.msgSuccess('提交成功')
            that.feedBackOpen = false
            that.$parent.getList();
          })
        }
      })
    }
  }
}
</script>
